/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.compomics.dataprovider.view.suite.panels.importing;

import com.compomics.dataprovider.control.logic.fasta.FastaSourceConverter;
import com.compomics.dataprovider.control.logic.storage.FastaManager;
import com.compomics.dataprovider.model.fasta.FastaStream;
import com.compomics.dataprovider.view.suite.model.FastaRepositoryProperties;
import java.awt.Color;
import java.io.File;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.apache.commons.io.FilenameUtils;

/**
 *
 * @author Kenneth
 */
public class FastaFileChooser extends JFileChooser {
    
    private final FastaFileOptions fileOptions =new FastaFileOptions();
    private final FileNameExtensionFilter filter = new FileNameExtensionFilter(
            "FASTA files", "fasta");
    
    public FastaFileChooser() {
        super();
        init();
    }
    
    private void init() {
        setAccessory(fileOptions);
        super.setBackground(Color.WHITE);
        setOpaque(true);
        setMultiSelectionEnabled(true);
        setFileFilter(filter);
    }
    
    
    public void importFiles(File folder) throws IOException {
        File[] selectedFiles = getSelectedFiles();
        FastaManager manager = new FastaManager(new File(FastaRepositoryProperties.getRepositoryPath()));
        fileOptions.isDecoysEnabled();
        fileOptions.isCleanupEnabled();
        for (File aFile : selectedFiles) {
            FastaStream aFastaStream = new FastaStream(FastaSourceConverter.convertToStream(aFile));
            if (fileOptions.isCrapEnabled()) {
                aFastaStream.appendCRAP();
            }
            if (fileOptions.isCrapEnabled()) {
                aFastaStream.addDecoys();
            }
            if (fileOptions.isCrapEnabled()) {
                aFastaStream.removeDuplicateAccessions().removeDuplicateSequences();
            }
            String extension = FilenameUtils.getExtension(aFile.getName());
            manager.storeFasta(aFile.getName().replace(extension,"fasta"),folder, aFastaStream);
        }
    }

    
}
